package com.su.mw.mscthesis;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.su.mw.mscthesis.service.MScThesisSubmissionService;
import com.su.mw.mscthesis.service.MScThesisSubmissionServiceService;
import com.su.mw.mscthesis.service.Status;
import com.su.mw.mscthesis.service.Thesis;
import com.su.mw.mscthesis.service.ThesisProposal;
import com.su.mw.mscthesis.service.ThesisReview;

public class MScThesisHandler {
	MScThesisSubmissionServiceService service;
	
	public MScThesisHandler() {
		super();
		service = new MScThesisSubmissionServiceService();
	}
	
	public Integer saveThesisProposal(String annotation, String description, String consultantName, String title, Integer executionTime, String programmingTechnologies, String student, String supervisor, String status, Date submitted) throws Exception {
		
		MScThesisSubmissionService port = service.getMScThesisSubmissionServicePort();
		ThesisProposal proposal = new ThesisProposal();
		proposal.setAnnotation(annotation);
		proposal.setDesription(description);
		proposal.setConsultant(consultantName);
		proposal.setTitle(title);
		proposal.setExecutionTime(executionTime);
		proposal.setProgramming(programmingTechnologies);
		proposal.setStudent(student);
		proposal.setSupervisor(supervisor);
		proposal.setStatus(Status.fromValue(status));
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(submitted);
		XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
		proposal.setSubmitted(date2);		
		return port.submitProposal(proposal);
	}
	
	public int updateProposalStatus(Integer proposalID, String status){
		MScThesisSubmissionService port = service.getMScThesisSubmissionServicePort();
		return port.updateProposalStatus(proposalID, Status.fromValue(status));
	}
	
	public void saveThesis(Integer thesisID, String department, String faculty, byte[] document, Date graduationDate) throws Exception {
		MScThesisSubmissionService port = service.getMScThesisSubmissionServicePort();
		Thesis thesis = new Thesis();
		thesis.setDepartment(department);
		thesis.setFaculty(faculty);
		thesis.setDocument(document);
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(graduationDate);
		XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
		thesis.setGraduationDate(date2);
		port.submitThesis(thesisID, thesis);
	}
	
	public Integer saveThesisReview(Integer thesisId, String comments, String conclusion, String contributions, String briefDescription, String layout, Integer mark, String recommendations, String relevance, String reviewer) {
		MScThesisSubmissionService port = service.getMScThesisSubmissionServicePort();
		ThesisReview thesisReview = new ThesisReview();
		thesisReview.setThesisID(thesisId);
		thesisReview.setComments(comments);
		thesisReview.setConclusion(conclusion);
		thesisReview.setContributions(contributions);
		thesisReview.setDescription(briefDescription);
		thesisReview.setLayout(layout);
		thesisReview.setMark(mark);
		thesisReview.setRecommendations(recommendations);
		thesisReview.setRelevance(relevance);
		thesisReview.setReviewer(reviewer);
		return port.submitThesisReview(thesisReview);
	}

}
